package com.lemon.smartsafety.ui.adapter.safe.nfcorqrcode;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.entity.safe.company.CompanyBuildingGroupVo;
import com.lemon.smartsafety.interfac.BackInterface;


import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 项目名称：SmartSafeCode
 * 类描述：
 * 创建人：Fargo
 * 创建时间：2016/12/22 14:40
 * 修改人：Fargo
 * 修改时间：2016/12/22 14:40
 * 修改备注：
 */
public class BuildItemAdapter extends BaseAdapter {

    /**
     * TAG
     */
    private final String TAG = "BuildItemAdapter";


    /**
     * 数据列表
     */
    private List<CompanyBuildingGroupVo> mList;
    /**
     * 选择的内容
     */
    private String strSelect = "";


    public String getStrSelect() {
        return strSelect;
    }

    public void setStrSelect(String strSelect) {
        this.strSelect = strSelect;
    }

    /**
     * 布局泵
     */
    private LayoutInflater m_layInflater = null;

    private Activity mActivity;

    private BackInterface.CallBackInterfaceInt callBackInterfaceInt = null;


    public void setCallBackInterfaceInt(BackInterface.CallBackInterfaceInt callBackInterfaceInt) {
        this.callBackInterfaceInt = callBackInterfaceInt;
    }


    /**
     * 设置数据列表
     */
    public void setList(List<CompanyBuildingGroupVo> list) {
        this.mList = list;
    }

    /**
     * 构造函数
     *
     * @param activity
     */
    public BuildItemAdapter(Activity activity, List<CompanyBuildingGroupVo> list) {
        try {
            this.mActivity = activity;

            this.mList = list;

            // 布局泵
            m_layInflater = LayoutInflater.from(activity);

        } catch (Exception e) {
        }
    }

    @Override
    public int getCount() {
        try {
            if (mList != null)
                return mList.size();
        } catch (Exception e) {
        }

        return 0;
    }

    @Override
    public Object getItem(int position) {
        try {
            if (mList != null)
                return mList.get(position);
        } catch (Exception e) {
        }

        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressWarnings({"deprecation"})
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        try {
            final ViewHolder holder;
            if (convertView == null) {
                convertView = m_layInflater.inflate(R.layout.item_center_title, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            //内容

            holder.textView.setText(mList.get(position).getGroupName());
            if (strSelect.equals(mList.get(position).getGroupName())) {
                holder.lineAll.setBackgroundResource(R.color.gray1);
            }else{
                holder.lineAll.setBackgroundResource(R.color.white);
            }
            holder.lineAll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (callBackInterfaceInt != null) {
                        callBackInterfaceInt.callBackFunction(position);
                    }
                }
            });



        } catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;
    }



    /**
     * 项支持器类
     */
    static class ViewHolder {
        @Bind(R.id.textView)
        TextView textView;
        @Bind(R.id.lineAll)
        LinearLayout lineAll;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
